package com.xumz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.xumz.controller.param.BlogParam;
import com.xumz.model.Teacher;
import com.xumz.service.TeacherService;


@Controller
public class TeacherController {
	@Autowired
	TeacherService service;
	
	public TeacherController() {
		System.out.println("TeacherController");
	}
	
	@RequestMapping(value = "/userValidate/{name}/{time}",method=RequestMethod.GET)
	public void userValidate(HttpServletRequest request, 
			HttpServletResponse response,Model model,@PathVariable("name") String name) throws IOException{
//		System.out.println("yonghuajiance");
		String Uname=new String(name.getBytes("ISO-8859-1"), "UTF-8");
		int key = service.validateName(Uname);
//		response.setContentType("text/html");
//		if (key== BlogParam.REGISTER_USERNAME_REPEAT) {
//			System.out.println("用户已经存在");
////			response.getWriter().print("用户已经存在");
//		}else{
//			System.out.println("用户可用");
//			response.getWriter().print("用户可用");
//		}
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		PrintWriter out =null;	
		//业务处理
		out = response.getWriter();
		if (key== BlogParam.REGISTER_USERNAME_REPEAT) {
//			System.out.println("用户已经存在");
			out.write("用户已经存在");
		}else{
//			System.out.println("用户可用");
			out.write("用户可用");
		}
		
	}
	
	@RequestMapping("/teacherLogin")
	public String login(Model model){
		return "teacher/admin/admin/login";
	}
	@RequestMapping("/teacherRegister")
	public String teacherRegister(Model model){
		return "teacher/admin/admin/register";
	}
	@RequestMapping("/teacherRegisterPost")
	public String teacherRegisterPost(Teacher teacher,Model model){
		int key = service.registerService(teacher);
		switch (key) {
		case BlogParam.REGISTER_USERNAME_REPEAT:
			model.addAttribute("usermsg", "用户名已存在");
			return "teacher/admin/admin/register";
		default:
			return "teacher/admin/admin/succ";
		}
	}
	@RequestMapping("/teacherIndexLeft")
	public String teacherIndexLeft(Model model){
		return "teacher/admin/iframe/left";
	}
	@RequestMapping(value = "/teacherIndexRight/{name}",method=RequestMethod.GET)
	public String Indexright(HttpServletRequest request,Model model,@PathVariable("name") String name) throws UnsupportedEncodingException{
		String Uname=new String(name.getBytes("ISO-8859-1"), "UTF-8");
		model.addAttribute("name", Uname);
		HttpSession session=request.getSession();
		session.setAttribute("name", Uname);
		return "teacher/admin/iframe/right";
	}
	
	@RequestMapping("/teacherIndexfirst")
	public String Indexfirst(Model model){
		return "teacher/admin/iframe/index1";
	}
	
	@RequestMapping(value = "/teacherIndex",method = {RequestMethod.POST})
	public String Index(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("pass")String pwd,
			 @RequestParam("user")String user){
		int key=service.loginService(user, pwd);
		switch (key) {
		case BlogParam.LONING_USER_NO_EXIT:
			model.addAttribute("usermsg", "不存在该用户");
			return "teacher/admin/admin/login";
		case BlogParam.LONING_USER_ERROR_PWD:
			model.addAttribute("pwdmsg", "密码错误");
			return "teacher/admin/admin/login";
		default:
			model.addAttribute("name", user);
			return "teacher/admin/iframe/index";
		}
		
	}

}
